package com.atguigu.crowd.service.impl;

import com.atguigu.crowd.entity.po.MemberConfirmInfoPO;
import com.atguigu.crowd.entity.po.MemberLaunchInfoPO;
import com.atguigu.crowd.entity.po.ProjectPO;
import com.atguigu.crowd.entity.po.ReturnPO;
import com.atguigu.crowd.entity.vo.*;
import com.atguigu.crowd.mapper.*;
import com.atguigu.crowd.service.api.ProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author cyb315
 * @create 2021-11-11 15:51
 */
// 在类上使用@Transactional(readOnly = true)针对查询操作设置事务属性
@Transactional(readOnly = true)
@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    private ProjectPOMapper projectPOMapper;

    @Autowired
    private ProjectItemPicPOMapper projectItemPicPOMapper;

    @Autowired
    private MemberLaunchInfoPOMapper memberLaunchInfoPOMapper;

    @Autowired
    private MemberConfirmInfoPOMapper memberConfirmInfoPOMapper;

    @Autowired
    private ReturnPOMapper returnPOMapper;

    /**
     * 保存发起项目信息
     *
     * @param projectVO
     * @param memberLoginVOId
     */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    @Override
    public void saveProject(ProjectVO projectVO, Integer memberLoginVOId) {

        // 一.保存ProjectVO对象
        // 1.1创建空对象
        ProjectPO projectPO = new ProjectPO();
        // 1.2把projectVO中的属性复制到projectPO中
        BeanUtils.copyProperties(projectVO, projectPO);
        // 修复bug，把memberLoginVOId设置到projectPO对象中
        projectPO.setMemberid(memberLoginVOId);
        // 修复bug，生成创建时间并设入
        String createDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        projectPO.setCreatedate(createDate);
        // 修复bug，status设置成0，表示即将开始
        projectPO.setStatus(0);
        // 1.3执行保存
        // 为了能获取到自增主键值，需要在ProjectPOMapper.xml中设置
        // <insert id="insertSelective" useGeneratedKeys="true" keyProperty="id" ......
        projectPOMapper.insertSelective(projectPO);
        // 1.4从projectPO对象获取自增主键
        Integer projectId = projectPO.getId();

        // 二.保存项目分类的关联关系信息
        // 2.1获取项目分类集合
        List<Integer> typeIdList = projectVO.getTypeIdList();
        // 2.2执行保存
        projectPOMapper.insertTypeRelationship(typeIdList, projectId);

        // 三.保存项目标签的关联关系信息
        List<Integer> tagIdList = projectVO.getTagIdList();
        projectPOMapper.insertTagRelationship(tagIdList, projectId);

        // 四.保存项目中的详情图片路径信息
        List<String> detailPicturePathList = projectVO.getDetailPicturePathList();
        projectItemPicPOMapper.insertPathList(projectId, detailPicturePathList);

        // 五.保存项目发起人信息
        MemberLauchInfoVO memberLauchInfoVO = projectVO.getMemberLauchInfoVO();
        MemberLaunchInfoPO memberLaunchInfoPO = new MemberLaunchInfoPO();
        // 5.1执行转换
        BeanUtils.copyProperties(memberLauchInfoVO, memberLaunchInfoPO);
        memberLaunchInfoPO.setMemberid(memberLoginVOId);
        // 5.2执行保存
        memberLaunchInfoPOMapper.insert(memberLaunchInfoPO);

        // 六.保存项目回报信息
//        List<ReturnVO> returnVOList = projectVO.getReturnVOList();
//        List<ReturnPO> returnPOList = new ArrayList<>();
//        for (ReturnVO returnVO : returnVOList) {
//            ReturnPO returnPO = new ReturnPO();
//            BeanUtils.copyProperties(returnVO, returnPO);
//            returnPOList.add(returnPO);
//        }
//        returnPOMapper.insertReturnPOBatch(returnPOList, projectId);


        // 七.保存项目确认信息
        MemberConfirmInfoVO memberConfirmInfoVO = projectVO.getMemberConfirmInfoVO();
        MemberConfirmInfoPO memberConfirmInfoPO = new MemberConfirmInfoPO();
        // 7.1执行转换
        BeanUtils.copyProperties(memberConfirmInfoVO, memberConfirmInfoPO);
        memberConfirmInfoPO.setMemberid(memberLoginVOId);
        // 7.2执行保存
        memberConfirmInfoPOMapper.insert(memberConfirmInfoPO);


    }

    /**
     * 获取所有项目类型集合实现类
     *
     * @return
     */
    @Override
    public List<PortalTypeVO> getPortalTypeVO() {
        return projectPOMapper.selectPortalTypeVOList();
    }

    /**
     * 获取指定项目详情集合
     *
     * @param projectId
     * @return
     */
    @Override
    public DetailProjectVO getDetailProjectVO(Integer projectId) {
        // 1.查询得到 DetailProjectVO 对象
        DetailProjectVO detailProjectVO = projectPOMapper.selectDetailProjectVO(projectId);
        // 2.根据 status 确定 statusText
        Integer status = detailProjectVO.getStatus();
        switch (status){
            case 0:
                detailProjectVO.setStatusText("审核中");
                break;
            case 1:
                detailProjectVO.setStatusText("众筹中");
                break;
            case 2:
                detailProjectVO.setStatusText("众筹成功");
                break;
            case 3:
                detailProjectVO.setStatusText("已关闭");
                break;
            default:
                break;
        }
        // 3.根据 deployeDate 计算 lastDay
        String deployDate = detailProjectVO.getDeployDate();
        // 获取当前日期
        Date currentDay = new Date();
        // 把众筹日期解析成 Date 类型
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date deployDay = simpleDateFormat.parse(deployDate);
            // 获取当前当前日期的时间戳
            long currentTimeStamp = currentDay.getTime();
            // 获取众筹日期的时间戳
            long deployTimeStamp = deployDay.getTime();
            // 两个时间戳相减计算当前已经过去的时间
            long pastDays = (currentTimeStamp-deployTimeStamp)/1000/60/60/24;
            //  获取总的众筹天数
            Integer totalDays = detailProjectVO.getDay();
            // 使用总的众筹天数减去已经过去的天数得到剩余天数
            Integer lastDay = (int)(totalDays-pastDays);
            // 将剩余天数设置进detailProjectVO对象
            detailProjectVO.setLastDay(lastDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return detailProjectVO;
    }
}
